/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.view;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import com.example.android.Utils;
import com.example.android.google.apis.MainActivity;
import com.example.android.google.apis.R;
import com.example.android.listeners.StartGestureButtonListener;
import com.example.android.logging.Logger;
import com.example.android.network.Client;
import com.example.android.synchronization.Updater;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class Map extends MapActivity
{
	
	final int serverPort = 50001;
	final int MAPZOOM = 18;
	
	private MapView mMapView;
	private MyLocationOverlay mMyLocationOverlay;
	private GeoPoint currentLocation;
	private Button startGestureButton;
	SensorManager sensorManager;
	Sensor accelerometer;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);	
		Logger.CurrentContext = this;

		setContentView(R.layout.mapview);
		mMapView = (MapView) findViewById(R.id.mapView);
		Updater.getInstance().onLogin(MainActivity.login);
		Updater.getInstance().setMapView(mMapView);
		Updater.getInstance().setMap(this);
		
		mMyLocationOverlay = new MyLocationOverlay(this, mMapView)
		{
			@Override
			public synchronized void onLocationChanged(Location location)
			{
				super.onLocationChanged(location);
				Updater.getInstance().onMyLocationChanged(Utils.locationToGeoPoint(location));
			}
		};
		mMyLocationOverlay.runOnFirstFix(new Runnable()
		{
			public void run()
			{
				mMapView.getController().animateTo(
						mMyLocationOverlay.getMyLocation());
				currentLocation = mMyLocationOverlay.getMyLocation();
			}
		});
		mMapView.getOverlays().add(mMyLocationOverlay);
		mMapView.getController().setZoom(MAPZOOM);
		mMapView.setClickable(true);
		mMapView.setEnabled(true);

		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		accelerometer = sensorManager
				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		startGestureButton = (Button) findViewById(R.id.startGestureButton);
		startGestureButton.setOnTouchListener(new StartGestureButtonListener(
				sensorManager, accelerometer));
	}

	@Override
	protected void onResume()
	{
		super.onResume();
		mMyLocationOverlay.enableMyLocation();
	}
	
	@Override
	public void onBackPressed()
	{
		super.onBackPressed();
		Client.getInstance().close();
	}

	@Override
	protected void onStop()
	{
		mMyLocationOverlay.disableMyLocation();
		super.onStop();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.optionsmenu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		CustomOverlay buildingsOverlay = null;
		currentLocation = mMyLocationOverlay.getMyLocation();
		switch (item.getItemId())
		{
		case R.id.fortressbutton:
			if (currentLocation != null)
			{
				buildingsOverlay = new CustomOverlay(currentLocation,
						R.drawable.fortress64);
			}
			break;
		case R.id.hospitalbuton:
			if (currentLocation != null)
			{
				buildingsOverlay = new CustomOverlay(currentLocation,
						R.drawable.redcross64);
			}
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		mMapView.getOverlays().add(buildingsOverlay);		
		Updater.getInstance().onBuildingConstructed(item.getItemId(), currentLocation);
		return true;
	}
	
	@Override
	protected void onDestroy()
	{	
		super.onDestroy();
	}

	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}
}